<?php

namespace Welcomescreen\Agavi\User;
use \Welcomescreen\Exceptions;

class SecurityUser extends \AgaviSecurityUser {
	public function login($username, $password) {
		$user = $this->getContext()->getModel('UserManager')->getUser($username);
		if($user && $user->checkPassword($password)) {
			$this->setAttribute('name', $user->getName());
			$this->setAuthenticated(true);
			return true;
		}
		$this->setAuthenticated(false);
		return false;
	}
	
	public function setAuthenticated($value) {
		parent::setAuthenticated($value);
		if(!$value) {
			// reset the stored user name if the user was deauthenticated
			$this->setAttribute('name', null);
		}
	}
	
	public function getUserObject() {
		if(!$this->isAuthenticated()) {
			throw new Exceptions\NotAuthenticatedException('The user needs to be logged in.');
		}
		
		return $this->getContext()->getModel('UserManager')->getUser($this->getAttribute('name'));
	}
}

?>